package com.cloud.api.base;



import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.reactive.function.server.ServerRequest;

import com.cloud.api.util.JwtTokenUtil;
import com.cloud.common.constants.CommonConstants;
import com.cloud.common.util.jwt.JWTHelper;
import com.cloud.common.util.jwt.RsaKeyHelper;



public class BaseController {
	public final static int PAGE_SIZE_BG = 1 ;
	public final static int PAGE_SIZE_TW = 20 ;
	public final static int PAGE_SIZE_FV = 50 ;
	public final static int PAGE_SIZE_HA = 100 ;
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
//	@Autowired
//	private ServerRequest request;
	
	@Autowired
	private JwtTokenUtil jwtTokenUtil;
	
	protected String getUserId(Map<String, String> reqeustHeader) {
		String userToken = getTokenFromHeader(reqeustHeader);
		try {
			Object userId = jwtTokenUtil.getInfoFromToken(userToken).getId();
			return (String)userId;
		} catch (Exception e) {
			logger.warn("token解析错误！",e);
			return null;
		}
	}
	
	protected String getTokenFromHeader(Map<String, String> reqeustHeader) {
		String userToken = reqeustHeader.get("gametoken");
		return userToken;
	}
	
	public static void main(String[] args) {
		try {
			String token = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJndXN0IiwidXNlcklkIjoiNWI4MzcxMWI2OWRhMWEwMzIyZDFkZmMwIiwibmFtZSI6Iua4uOWuoiIsImV4cCI6MTUzNTg0MDgyOH0.B5v7G8VzZLVjhJWKFtTLV671ENdkiE8vO17ZwRFkLXKQ0KTCXgT1jtOAPf1LzgBiz_dZpEV6dJFpgbS_u44O1UpwSdRLS8AmrnpKvsehQGJc-a_CILZ6qJXkb5-QOVUiPpGNNqu5Ph7L0YWPSfW-xXHojt-DKlrGxHxAGKnOcFE";
//			String pub1=new String(RsaKeyHelper.generateKey(CommonConstants.JWT_SECURITY).get("pub"),"UTF-8");
//			System.out.println(pub1);
//			System.out.println("----------");
//			String pub2=new String(RsaKeyHelper.generateKey(CommonConstants.JWT_SECURITY).get("pub"),"UTF-8");
//			System.out.println(pub2);
			Object userId = JWTHelper.parserToken(token, CommonConstants.JWT_SECURITY.getBytes()).getBody().get("userId");
			System.out.println(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
